import os
import json
from default_prompts_en import preset_prompts

def generate_key_value_json(out_file, prompts):
    '''
    Generate a JSON file with key-value pairs.

    Args:
        out_file(str):  path to the output JSON file
        prompts(dict):  a dictionary containing key-value pairs
    
    Returns:
        None
    '''
    prompts_str = json.dumps(prompts, indent=4, ensure_ascii=False)
    with open(out_file, 'w') as f:
        f.write(prompts_str)

def kv_pair_formatter(prompts):
    '''
    Format a dictionary into a list of key-value pairs.

    Args:
        prompts(dict):  a dictionary containing key-value pairs
    
    Returns:
        format_prompts(list):   a list of dictionary, each dictionary contains 'key' and 'value' fields
    '''
    format_prompts = []
    for key in prompts:
        format_prompts.append({'key': key, 'value': prompts[key]})
    return format_prompts

def strip_preprocess(prompts):
    '''
    Strip whitespace from all values in a dictionary.

    Args:
        prompts(dict):     a dictionary containing key-value pairs
    
    Returns:
        prompts(dict):     the same dictionary with each value stripped
    '''
    for key in prompts:
        prompts[key] = prompts[key].strip()
    return prompts

def subfix_preprocess(prompts):
    '''
    Add a suffix to all values in a dictionary.

    Args:
        prompts(dict):     a dictionary containing key-value pairs
    
    Returns:
        prompts(dict):     the same dictionary with a suffix added to each value
    '''
    for key in prompts:
        prompts[key] = prompts[key] + " My first question is: \n"
    return prompts

def main():
    prompts = strip_preprocess(preset_prompts)
    format_sys_prompts = kv_pair_formatter(prompts)
    generate_key_value_json('sys_default_en.json', format_sys_prompts)

    user_prompts = subfix_preprocess(prompts)
    format_user_prompts = kv_pair_formatter(user_prompts)
    generate_key_value_json('default_en.json', format_user_prompts)

if __name__ == '__main__':
    main()
